///
/// 列表案例
///
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  int _bottomSelectedIndex = 0; // 底部导航栏选中的Tab索引
  int _drawerSelectedIndex = 0; // 抽屉选中的Tab索引


  // 定义抽屉的Tab页面
  static const List<Widget> _drawerWidgetOptions = <Widget>[
    ListViewApp(),
    GridViewApp(),
    Text('抽屉我的内容'),
  ];

  void _onBottomItemTapped(int index) {
    setState(() {
      _bottomSelectedIndex = index;
    });
  }

  void _onDrawerItemTapped(int index) {
    setState(() {
      _drawerSelectedIndex = index;
    });
    Navigator.pop(context); // 关闭抽屉
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text('主页面'),
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: () {
            _scaffoldKey.currentState?.openDrawer();
          },
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.info),
            onPressed: () {
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: Text('提示'),
                    content: Text('你好'),
                    actions: <Widget>[
                      TextButton(
                        child: Text('关闭'),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                  );
                },
              );
            },
          ),
        ],
      ),
      drawer: Drawer(
        child: ListView(
          children: <Widget>[
            ListTile(
              title: Text('首页'),
              selected: _drawerSelectedIndex == 0,
              onTap: () {
                _onDrawerItemTapped(0);
              },
            ),
            ListTile(
              title: Text('分类'),
              selected: _drawerSelectedIndex == 1,
              onTap: () {
                _onDrawerItemTapped(1);
              },
            ),
            ListTile(
              title: Text('我的'),
              selected: _drawerSelectedIndex == 2,
              onTap: () {
                _onDrawerItemTapped(2);
              },
            ),
          ],
        ),
      ),
      body: Column(
        children: [
          Center(
            child: _drawerWidgetOptions.elementAt(_drawerSelectedIndex),
          ),
        ],
      ),
    );
  }
}

///
/// 垂直列表
///
class ListViewApp extends StatelessWidget {
  const ListViewApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          ListTile(
            leading: Icon(Icons.assessment,color: Colors.red),
            title: Text('我是标题1'),
          ),
          const Divider(),
          ListTile(
            leading: Image.network('https://www.itying.com/images/flutter/1.png'),
            title: const Text('华北黄淮高温雨今起强势登场'),
            subtitle: const Text('中国天气网讯 21日开始，华北黄淮高温雨今起强势登场'),
          ),
          const Divider(),
          ListTile(
            leading: Image.network('https://www.itying.com/images/flutter/2.png'),
            title: const Text('保监局50天开32罚单 “断供”违规资金为房市降温'),
            subtitle: const Text('中国天气网讯 保监局50天开32罚单 “断供”违规资金为房市降温'),
          ),
          ListTile(
            contentPadding: EdgeInsets.zero, // 移除ListTile的默认内边距
            title: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Image.network(
                  'https://www.itying.com/images/flutter/1.png',
                  width: double.infinity, // 图片宽度充满整个ListTile
                  height: 100, // 设置图片的高度
                  fit: BoxFit.cover, // 图片填充方式
                ),
                SizedBox(height: 8), // 图片和文字之间的间距
                Center(child: Text('图片标题'),),
              ],
            ),
          ),
          ListTile(
            title: Text('我是标题1'),
          )
        ],
      ),
    );
  }
}

///
/// 网格布局
///
class GridViewApp extends StatelessWidget {
  const GridViewApp({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        height: 300, child:  GridView.count(
      crossAxisCount: 3,
      childAspectRatio: 1.0,
      children: const <Widget>[
        Icon(Icons.home),
        Icon(Icons.ac_unit),
        Icon(Icons.search),
        Icon(Icons.airport_shuttle),
        Icon(Icons.all_inbox),
        Icon(Icons.cake),
      ],
    )
    );
  }
}